package com.lxyblog2030.blog.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyblog2030.blog.pojo.Type;
import com.lxyblog2030.blog.queryvo.FirstPageBlog;
import com.lxyblog2030.blog.service.BlogService;
import com.lxyblog2030.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TypeShowController {

    @Autowired
   private BlogService blogService;
    @Autowired
    private TypeService typeService;


    @GetMapping("/types/{id}")
    public String types(@PathVariable long id, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model){
        List<Type> types = typeService.getAllType();
        if (id==-1){
            id= types.get(0).getId();
        }
        model.addAttribute("types",types);
        List<FirstPageBlog> blogs = blogService.getByTypeId(id);
        PageHelper.startPage(pageNum,100);
        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeTypeId",id);
        return "types";


    }
}
